﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Xml = System.Xml;

namespace proj002
{
    class XPaidXml
    {
        private Xml.XmlDocument m_doc;
        private Xml.XmlNode m_root;
        private Xml.XmlElement m_currencyElement;
        private Xml.XmlElement m_instrumentElement;
       
        System.String m_filePath;

        public XPaidXml(System.String in_filePath)
        {
            //Todo: check file 
            m_doc = new Xml.XmlDocument();
            m_filePath = in_filePath;
            setRoot();
        }

        public void setRoot()
        {
            // e.g. 
            // <paid.xls>
            // ...
            // </paid.xls>
            m_root = m_doc.CreateNode(Xml.XmlNodeType.Element, Path.GetFileName(m_filePath),"");
            m_doc.AppendChild(m_root);
        }

        public void setCurrencyNode(System.String strCurrency)
        {
            m_currencyElement = m_doc.CreateElement("Currency");
            System.String[] words = strCurrency.Split(':');
            System.String type = words[1].Trim();

            m_currencyElement.SetAttribute("type", type);
            Xml.XmlText currencyText = m_doc.CreateTextNode(strCurrency);
            m_root.AppendChild(m_currencyElement);
        }

        public void setInstrumentNode(System.String strInstrumentNode)
        {
            m_instrumentElement = m_doc.CreateElement("Instrument");
            //Xml.XmlText instrumentText = m_doc.CreateTextNode(strInstrumentNode);
            System.String[] words = strInstrumentNode.Split(':');
            System.String type = words[1].Trim();
            m_instrumentElement.SetAttribute("type", type);
            m_currencyElement.AppendChild(m_instrumentElement);
        }

        public void setRecord(object[] record)
        {
            Xml.XmlElement logicalNumber = m_doc.CreateElement("LogicalNumber");
            Xml.XmlElement companyName = m_doc.CreateElement("CompanyName");
            Xml.XmlElement maturityDate = m_doc.CreateElement("MaturityDate");
            Xml.XmlElement totalAmount = m_doc.CreateElement("TotalAmount");
            Xml.XmlElement paidStatus = m_doc.CreateElement("PaidStatus");
            Xml.XmlElement localCurrency = m_doc.CreateElement("LocalCurrency");
            Xml.XmlElement localAmount = m_doc.CreateElement("LocalAmount");
            Xml.XmlElement rate = m_doc.CreateElement("Rate");

            logicalNumber.InnerText = record[0].ToString();
            m_instrumentElement.AppendChild(logicalNumber);

            companyName.InnerText = record[1].ToString();
            m_instrumentElement.AppendChild(companyName);

            maturityDate.InnerText = record[2].ToString();
            m_instrumentElement.AppendChild(maturityDate);

            totalAmount.InnerText = record[3].ToString();
            m_instrumentElement.AppendChild(totalAmount);

            paidStatus.InnerText = record[4].ToString();
            m_instrumentElement.AppendChild(paidStatus);

            localCurrency.InnerText = record[5].ToString();
            m_instrumentElement.AppendChild(localCurrency);

            localAmount.InnerText = record[6].ToString();
            m_instrumentElement.AppendChild(localAmount);

            rate.InnerText = record[7].ToString();
            m_instrumentElement.AppendChild(rate);

        }

        public void saveXmlFile()
        {
            try
            {
                m_doc.Save("c:\\paid.xml");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
